interface SearchOpt {
  [key: string]: string | number;
}

export const queryToString = (search: SearchOpt = {}) => {
  const searchArr: string[] = [];
  const keys = Object.keys(search);

  keys.forEach((key) => {
    if (search[key] !== undefined && search[key] !== '') {
      searchArr.push(`${key}=${search[key]}`);
    }
  });

  return searchArr.join('&');
};

export const createPath = (path: string, query: SearchOpt = {}) => {
  const queryString = queryToString(query);
  let linkCode = '?';

  if (path.includes('?')) {
    linkCode = '&';
  }
  return `${path}${linkCode}${queryString}`;
};
